package com.never;

import org.apache.flink.api.common.functions.FoldFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.wikiedits.WikipediaEditEvent;
import org.apache.flink.streaming.connectors.wikiedits.WikipediaEditsSource;







/**
 * Demo：模拟监控维基百科日志
 * @author : zhangbinbin
 * @date :  2019/11/23  16:45
 * @description:
 */
public class WikipediaAnalysis {
    public static void main(String[] args) throws Exception {
        /** (1) 创建Flink 执行环境上下文 */
        StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();

        /**（2）添加Flink source 来源*/
        DataStream<WikipediaEditEvent> edits = see.addSource(new WikipediaEditsSource());

        /** (3) Transformation */
        KeyedStream<WikipediaEditEvent, String> keyedEdits = edits
                .keyBy(new KeySelector<WikipediaEditEvent, String>() {
                    @Override
                    public String getKey(WikipediaEditEvent event) {
                        return event.getUser();
                    }
                });
        /** (4) 创建窗口 */
        DataStream<Tuple2<String, Long>> result = keyedEdits
                .timeWindow(Time.seconds(5)) // 指定窗口宽度
                .fold(new Tuple2<>("", 0L), new FoldFunction<WikipediaEditEvent, Tuple2<String, Long>>() {
                    @Override
                    public Tuple2<String, Long> fold(Tuple2<String, Long> acc, WikipediaEditEvent event) {
                        acc.f0 = event.getUser();
                        acc.f1 += event.getByteDiff();
                        return acc;
                    }
                });

        /** (5) 数据输出*/
        result.print();
        see.execute();
    }
}
